package com.boot.security.service.cluster;

import com.boot.security.model.po.Resource;
import com.boot.security.model.vo.SourceKey;
import com.boot.security.service.authorization.AccessSourceClusterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description 消息队列通知更新 {@link StreamAccessSourceClusterHandlerImpl}
 * @date 2021/8/1 22:28
 **/

@Slf4j
@Service
public class StreamAccessSourceClusterServiceImpl implements AccessSourceClusterService {

    private final StreamBridge streamBridge;
    private final static String BINDING_NAME = "security_access_source_cluster_service-out-0";

    public StreamAccessSourceClusterServiceImpl(StreamBridge streamBridge) {
        this.streamBridge = streamBridge;
    }

    @Override
    public void saveOne(SourceKey sourceKey, Long resourceId) {
        AccessSourceClusterChange change = AccessSourceClusterChange.builder()
                .sourceKey(sourceKey)
                .type(AccessSourceClusterChange.SAVE_ONE)
                .resourceId(resourceId)
                .build();
        Message<AccessSourceClusterChange> message = MessageBuilder.withPayload(change).build();
        streamBridge.send(BINDING_NAME, message);
    }

    @Override
    public void saveMulti(SourceKey sourceKey, List<Long> resourceIds) {
        AccessSourceClusterChange change = AccessSourceClusterChange.builder()
                .sourceKey(sourceKey)
                .type(AccessSourceClusterChange.SAVE_MULTI)
                .resourceIds(resourceIds)
                .build();
        Message<AccessSourceClusterChange> message = MessageBuilder.withPayload(change).build();
        streamBridge.send(BINDING_NAME, message);
    }

    @Override
    public void updateOne(SourceKey sourceKey, Resource oldResource) {
        AccessSourceClusterChange change = AccessSourceClusterChange.builder()
                .sourceKey(sourceKey)
                .type(AccessSourceClusterChange.UPDATE_ONE)
                .resource(oldResource)
                .build();
        Message<AccessSourceClusterChange> message = MessageBuilder.withPayload(change).build();
        streamBridge.send(BINDING_NAME, message);
    }

    @Override
    public void removeOne(SourceKey sourceKey, Resource oldResource) {
        AccessSourceClusterChange change = AccessSourceClusterChange.builder()
                .sourceKey(sourceKey)
                .type(AccessSourceClusterChange.REMOVE_ONE)
                .resource(oldResource)
                .build();
        Message<AccessSourceClusterChange> message = MessageBuilder.withPayload(change).build();
        streamBridge.send(BINDING_NAME, message);
    }

    @Override
    public void removeMulti(SourceKey sourceKey, List<Resource> resources) {
        AccessSourceClusterChange change = AccessSourceClusterChange.builder()
                .sourceKey(sourceKey)
                .type(AccessSourceClusterChange.REMOVE_MULTI)
                .resources(resources)
                .build();
        Message<AccessSourceClusterChange> message = MessageBuilder.withPayload(change).build();
        streamBridge.send(BINDING_NAME, message);
    }
}
